Setext headings
A setext heading consists of one or more lines of text, each containing at least one non-whitespace character, with no more than 3 spaces indentation, followed by a setext heading underline. The lines of text must be such that, were they not followed by the setext heading underline, they would be interpreted as a paragraph: they cannot be interpretable as a code fence, ATX heading, block quote, thematic break, list item, or HTML block.
A setext heading underline is a sequence of =
characters or a sequence of -
characters, with no more than 3 spaces indentation and any number of trailing spaces. If a line containing a single -
can be interpreted as an empty list items, it should be interpreted this way and not as a setext heading underline.
The heading is a level 1 heading if =
characters are used in the setext heading underline, and a level 2 heading if -
characters are used. The contents of the heading are the result of parsing the preceding lines of text as CommonMark inline content.
In general, a setext heading need not be preceded or followed by a blank line. However, it cannot interrupt a paragraph, so when a setext heading comes after a paragraph, a blank line is needed between them.
Simple examples:
Example 50
Markdown | HTML | Demo |
---|---|---|
|
|
The content of the header may span more than one line:
Example 51
Markdown | HTML | Demo |
---|---|---|
|
|
Example 52
Markdown | HTML | Demo |
---|---|---|
|
|
The underlining can be any length:
Example 53
Markdown | HTML | Demo |
---|---|---|
|
|
The heading content can be indented up to three spaces, and need not line up with the underlining:
Example 54
Markdown | HTML | Demo |
---|---|---|
|
|
Four spaces indent is too much:
Example 55
Markdown | HTML | Demo |
---|---|---|
|
|
The setext heading underline can be indented up to three spaces, and may have trailing spaces:
Example 56
Markdown | HTML | Demo |
---|---|---|
|
|
Four spaces is too much:
Example 57
Markdown | HTML | Demo |
---|---|---|
|
|
The setext heading underline cannot contain internal spaces:
Example 58
Markdown | HTML | Demo |
---|---|---|
|
|
Trailing spaces in the content line do not cause a line break:
Example 59
Markdown | HTML | Demo |
---|---|---|
|
|
Nor does a backslash at the end:
Example 60
Markdown | HTML | Demo |
---|---|---|
|
|
Since indicators of block structure take precedence over indicators of inline structure, the following are setext headings:
Example 61
Markdown | HTML | Demo |
---|---|---|
|
|
The setext heading underline cannot be a lazy continuation line in a list item or block quote:
Example 62
Markdown | HTML | Demo |
---|---|---|
|
|
Example 63
Markdown | HTML | Demo |
---|---|---|
|
|
Example 64
Markdown | HTML | Demo |
---|---|---|
|
|
A blank line is needed between a paragraph and a following setext heading, since otherwise the paragraph becomes part of the heading’s content:
Example 65
Markdown | HTML | Demo |
---|---|---|
|
|
But in general a blank line is not required before or after setext headings:
Example 66
Markdown | HTML | Demo |
---|---|---|
|
|
Setext headings cannot be empty:
Example 67
Markdown | HTML | Demo |
---|---|---|
|
|
Setext heading text lines must not be interpretable as block constructs other than paragraphs. So, the line of dashes in these examples gets interpreted as a thematic break:
Example 68
Markdown | HTML | Demo |
---|---|---|
|
|
Example 69
Markdown | HTML | Demo |
---|---|---|
|
|
Example 70
Markdown | HTML | Demo |
---|---|---|
|
|
Example 71
Markdown | HTML | Demo |
---|---|---|
|
|
If you want a heading with > foo
as its literal text, you can use backslash escapes:
Example 72
Markdown | HTML | Demo |
---|---|---|
|
|
Compatibility note: Most existing Markdown implementations do not allow the text of setext headings to span multiple lines. But there is no consensus about how to interpret
Foo
bar
---
baz
One can find four different interpretations:
- paragraph “Foo”, heading “bar”, paragraph “baz”
- paragraph “Foo bar”, thematic break, paragraph “baz”
- paragraph “Foo bar — baz”
- heading “Foo bar”, paragraph “baz”
We find interpretation 4 most natural, and interpretation 4 increases the expressive power of CommonMark, by allowing multiline headings. Authors who want interpretation 1 can put a blank line after the first paragraph:
Example 73
Markdown | HTML | Demo |
---|---|---|
|
|
Authors who want interpretation 2 can put blank lines around the thematic break,
Example 74
Markdown | HTML | Demo |
---|---|---|
|
|
or use a thematic break that cannot count as a setext heading underline, such as
Example 75
Markdown | HTML | Demo |
---|---|---|
|
|
Authors who want interpretation 3 can use backslash escapes:
Example 76
Markdown | HTML | Demo |
---|---|---|
|
|